﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGame.ServerObjects
{

    class Database
    {
        public Player OwningPlayer;
        public Dictionary<string, Dictionary<string, string>> Tables = new Dictionary<string, Dictionary<string, string>>();

        public Database(Player ply)
        {
            this.OwningPlayer = ply;
        }

        public void SetKey(string table, string key, string value)
        {
            if (!this.Tables.ContainsKey(table))
            {
                this.Tables.Add(table, new Dictionary<string, string>());
            }
            if (!this.Tables[table].ContainsKey(key))
            {
                this.Tables[table].Add(key, value);
            }
            else
            {
                this.Tables[table][key] = value;
            }
        }

        public string GetKey(string table, string key)
        {
            if (!this.Tables.ContainsKey(table))
            {
                return "Table does not Exist";
            }
            if (!this.Tables[table].ContainsKey(key))
            {
                return "Error";
            }
            else
            {
                return "OK - " + this.Tables[table][key];
            }
        }

        public string AddValue(string table, string value)
        {
            if (!this.Tables.ContainsKey(table))
            {
                return "Table does not Exist";
            }
            this.Tables[table].Add(this.Tables[table].Count.ToString(), value);
            return "";
        }

        public string GetAllKeys(string table)
        {
            if (!this.Tables.ContainsKey(table))
            {
                return "Table does not Exist";
            }
            string str = "";
            foreach (string item in this.Tables[table].Keys)
            {
                str += item + " ";
            }
            return str;
        }
    }
}
